package com.wondersgroup.insurance.dnuilearn.views.shader;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.RadialGradient;
import android.graphics.Shader;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.AccelerateInterpolator;

/**
 * Created by Administrator on 2017/5/12.
 */

public class WaterWaveView extends View {
    private int[] mColors = {Color.WHITE, 0xFF58FAAC};


    int cenetX,centerY;
    int radius;
   Paint mPaint;
    // 默认半径
    private int DEFAULT_RADIUS = 50;
    public WaterWaveView(Context context) {
        this(context, null);
    }

    public WaterWaveView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public WaterWaveView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setLayerType(LAYER_TYPE_SOFTWARE,null);
        mPaint = new Paint();
    }



    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
//        cenetX = w/2;
//        centerY = h/2;
//        radius = Math.min(centerY,cenetX) -30;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawCircle(cenetX, centerY, radius, mPaint);

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (cenetX != event.getX() || centerY != event.getY()){
            cenetX = (int) event.getX();
            centerY = (int) event.getY();
            setRadius(DEFAULT_RADIUS);
        }

        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                return true;
//            case MotionEvent.ACTION_CANCEL:
            case MotionEvent.ACTION_UP:
            {
                if (mAnimator != null && mAnimator.isRunning()) {
                    mAnimator.cancel();
                }
                if (mAnimator == null) {
                    mAnimator = ObjectAnimator.ofInt(this,"radius",DEFAULT_RADIUS, getWidth());
                }

                mAnimator.setInterpolator(new AccelerateInterpolator());
                mAnimator.addListener(new Animator.AnimatorListener() {
                    @Override
                    public void onAnimationStart(Animator animation) {

                    }

                    @Override
                    public void onAnimationEnd(Animator animation) {
                        setRadius(0);
                    }

                    @Override
                    public void onAnimationCancel(Animator animation) {

                    }

                    @Override
                    public void onAnimationRepeat(Animator animation) {

                    }
                });
                mAnimator.start();
            }

                break;
        }
        return super.onTouchEvent(event);
    }

    void setRadius(int rad){
        radius = rad;
        if (rad > 0){

            RadialGradient mRadialGradient = new RadialGradient(cenetX, centerY, radius, mColors, null, Shader.TileMode.REPEAT);
            mPaint.setShader(mRadialGradient);
        }
        postInvalidate();
    }

    private ObjectAnimator mAnimator;
}
